import subprocess
from Log import logger

"""
对subprocess模块进行封装，带日志输出
"""
def subprocess_Popen(m4s1,m4s2,mp4):
    # FFmpeg命令行参数 , ' -c copy '
    command = ['ffmpeg', '-i', m4s1, '-i', m4s2, mp4]
    try:
        # 运行FFmpeg命令
        output = subprocess.check_output(command)
        logger.info("转换成功！")
    except subprocess.CalledProcessError as e:
        logger.error("转换失败：", e.returncode)
        logger.error("错误信息：", e.output)


def subprocess_Popen_old20240205(cmd):
    try:
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
        while p.poll() is None:
            line = p.stdout.readline()
            line = line.strip()
            if line:
                logger.info('Subprocess output: [{}]'.format(line))
            err = p.stderr.readline()
            err = err.strip()
            if err:
                logger.debug('p.stderr:[{}]'.format(err))
        if p.returncode == 0:
            pass
        else:
            logger.error('{0}->失败!'.format(cmd))
    except Exception as e:
        logger.error(cmd)
        logger.error(e)

